<?php
namespace plugins\address\model;

use think\Model;

/**
 * 地址模型
 */
class AddressModel extends Model
{
    function getTree($data, $code_arr=[],$parent_code = '330000')
    {
        $arr = [];
        foreach($data as $key => $val){
            if(in_array($val['cri_code'], $code_arr, false)){
                $val['disabled'] = true;
            }

            if($val['cri_superior_code'] == $parent_code){
                $val['children'] = $this->getTree($data,$code_arr, $val['cri_code']);
                if(empty($val['children'])){
                    unset($val['children']);
                }
                $arr[] = $val;
            }
        }
        return $arr;
    }

    public function getLowLevel($cri_code){
        $id_arr = [];
        $list = $this->where('cri_superior_code',$cri_code)->select();
        foreach ($list as $item){
            $id_arr[] = $item['cri_code'];
            $children = $this->getLowLevel($item['cri_code']);
            $id_arr = array_merge($id_arr,$children);
        }
        $id_arr[] = $cri_code;
        return $id_arr;
    }
}
